﻿@page
@using SXFramWork.Web
@{
    ViewData["Title"] = "字典管理";
    Layout = AdminLayout.Pjax(HttpContext);
}
    <div id="container">
        <div class="list-wall">
            <div class="layui-form list-search" >
                <button type="button" class="layui-btn layui-btn-sm layui-hide" data-type="reload">
                    <i class="layui-icon layui-icon-refresh"></i> 刷新
                </button>

                <button type="button" class="layui-btn layui-btn-normal" data-type="toolAdd"><i class="layui-icon"></i>添加</button>
                <button type="button" class="layui-btn layui-btn-danger" data-type="toolDel"><i class="layui-icon"></i> 删除</button>
            </div>

            @*//表格*@
            <table class="layui-hide" id="tablist" lay-filter="tool"></table>

        </div>

        @* 行内工具栏*@
        <script type="text/html" id="tool">

            <a class="layui-btn  layui-btn-warm " lay-event="edit" action-code="admin_sysuser_update"> 修改</a>

        </script>

        <script>
            layui.use([ 'layer', 'jquery', 'common', 'treeTable'],
                function () {
                    var layer = layui.layer,
                        $ = layui.jquery,
                        os = layui.common;
                    treeTable = layui.treeTable;
                    var rendertree = os.treeTableRender({
                        elem: '#tablist',
                        url: '/api/admin/sysdictionary/list',
                        cols: [
                            [
                                { type: 'checkbox', fixed: 'left' },
                                { field: 'name', title: '名称' },
                                { field: 'code', title: '编码' },
                                { field: 'value', title: '字典值' },
                                { field: 'sort', title: '排序', sort: true },
                                { field: 'createTime', title: '创建时间' },
                                { title: '操作', templet: '#tool',width:160 }
                            ]
                        ],
                        //数据渲染完的回调
                        done: function () {
                            //关闭加载
                            layer.closeAll('loading');
                            //默认展开全部
                            //rendertree.expandAll();
                        }

                    })

                    var guid = '', active = {
                        reload: function () {
                            //刷新表格
                            os.treeTableReload(rendertree, {
                                where: {},
                            }); 
                        },
                        toolAdd: function () {
                            os.Open('添加字典', '/Admin/SysDictionary/Add?opt=add', '600px', '390px', function () {
                                
                            });
                        },
                        toolDel: function () {

                            var data = rendertree.checkStatus(false);

                            //console.log(data);

                            if (data.length === 0) {
                                os.error("请选择要删除的项目！");
                                return;
                            }
                            var ids = '';
                            $.each(data, function (i, item) {
                                ids += item.id + ",";
                            });
                            layer.confirm('确定要执行批量删除吗？', function (index) {
                                layer.close(index);

                                ids = ids.substring(0, ids.length - 1);
                                os.ajax('api/admin/sysdictionary/delete',  ids , function (res) {
                                  
                                    if (res.code === 1) {
                                        active.reload();
                                        os.success(res.msg);
                                    } else {
                                        os.error(res.message);
                                    }
                                },'post');
                            });

                        }
                    };

                    $('.list-search .layui-btn').on('click', function () {
                        var type = $(this).data('type');
                        active[type] ? active[type].call(this) : '';
                    });

                    //监听工具条
                    treeTable.on('tool(tablist)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'edit') {
                            os.Open('字典修改', '/Admin/SysDictionary/Add/?opt=edit&id=' + data.id, '600px', '390px', function () {
                                
                            });
                        }
                    });
                });
        </script>

    </div>
